package ua.preloader
{	
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.system.Capabilities;
	import flash.text.StaticText;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.*;
	
	import mx.events.*;
	import mx.preloaders.*;
	
	public class CustomPreloader extends Sprite 
		implements IPreloaderDisplay
	{
		[ Embed(source="assets/logo2.jpg", mimeType="application/octet-stream") ]
		private var myLogo:Class;
		public var timer:Timer;
		private var logoLoader:flash.display.Loader;
		//----------------------------------------------------------
		// UI
		//----------------------------------------------------------
		private var X:int = 300;
		private var Y:int = 100;
		private var W:int = 600;
		private var H:int = 400;
		private var border:int = 1;
		private var progressBar:Shape = new Shape();
		
		public function CustomPreloader() {   
			super();        
		}
		
		// Specify the event listeners.
		public function set preloader(preloader:Sprite):void {
			preloader.addEventListener(
				FlexEvent.INIT_PROGRESS, handleInitProgress);
			preloader.addEventListener(
				FlexEvent.INIT_COMPLETE, handleInitComplete);
			preloader.addEventListener(ProgressEvent.PROGRESS, drawProgress);
		}
		
		public function initialize():void {
			logoLoader = new flash.display.Loader();       
			logoLoader.contentLoaderInfo.addEventListener(
				Event.COMPLETE, logoLoaderComplete);
			logoLoader.visible = false;
			logoLoader.loadBytes( new myLogo() as ByteArray );
			logoLoader.x = X;
			logoLoader.y = Y;
			addChild(logoLoader);
			addChild(progressBar);
		}
		
		public function drawProgress(event:ProgressEvent):void{
			var g:Graphics = progressBar.graphics;
			var persent:Number = event.bytesLoaded/event.bytesTotal;
			g.beginFill(0x000000);
			g.lineStyle(1, 0xE85115, 1.0);
			g.drawRect(X + 10, Y + H -20, (W-20)*persent, 2);
			g.endFill();
		}

		private function logoLoaderComplete(event:Event):void
		{
			logoLoader.stage.addChild(this)
			X = logoLoader.stage.stageWidth/2 - W/2;
			Y = logoLoader.stage.stageHeight/2 - H/2;
			logoLoader.visible=true;
		}   
		
		private function handleInitProgress(event:FlexEvent):void {
		}

		private function handleInitComplete(event:Event):void {
			var timer:Timer = new Timer(3000,1);
			timer.addEventListener(TimerEvent.TIMER, dispatchComplete);
			timer.start();    
		}
		
		private function dispatchComplete(event:TimerEvent):void {
			dispatchEvent(new Event(Event.COMPLETE));
			closeScreen();  
		}
		
		public function closeScreen():void
		{
			timer = new Timer( 1 );
			timer.addEventListener( TimerEvent.TIMER, closeScreenFade );                    
			timer.start();
		}
		
		public function closeScreenFade( event:TimerEvent ):void
		{
			if( logoLoader.alpha > 0){
				for(var i:int = 0; i < numChildren; i++){
					getChildAt(i).alpha -= .02;
				}
				this.alpha = this.alpha - .02;
			} else {
				timer.stop()
				this.removeChild(logoLoader);
			}        
		}        
		
		public function get backgroundColor():uint {
			return 0;
		}
		
		public function set backgroundColor(value:uint):void {
		}
		
		public function get backgroundAlpha():Number {
			return 0;
		}
		
		public function set backgroundAlpha(value:Number):void {
		}
		
		public function get backgroundImage():Object {
			return null;
		}
		
		public function set backgroundImage(value:Object):void {
		}
		
		public function get backgroundSize():String {
			return "";
		}
		
		public function set backgroundSize(value:String):void {
		}
		
		public function get stageWidth():Number {
			return 0;
		}
		
		public function set stageWidth(value:Number):void {
		}
		
		public function get stageHeight():Number {
			return 0;
		}
		
		public function set stageHeight(value:Number):void {
		}
	}
}